package com.example.demo2.controller;

import com.example.demo2.mapper.AddressMapper;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.data.AddressDO;
import com.example.demo2.pojo.vo.AddressVO;
import com.example.demo2.pojo.vo.SupplierVO;
import com.example.demo2.service.AddressService;
import com.example.demo2.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class AddressController {
    @Resource
    AddressService addressService;
    @RequestMapping("/address_list")
    public String supplier(String name, @RequestParam(defaultValue = "1") Integer currentPage, Model model) throws Exception {
        AddressVO addressVO=new AddressVO();
        addressVO.setEquipmentName(name);
        addressVO.setCurrentPage(currentPage);
        PageBean pageBean=addressService.listPage(addressVO);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("query", addressVO);
        return "address_list";
    }
    @RequestMapping("/address_list/add")
    @ResponseBody
    //形式参数名要和前端name属性里的一样
    public Result add(String equipmentname, Integer enumber, String location)throws Exception{
        AddressVO addressVO=new AddressVO();
        addressVO.setEquipmentName(equipmentname);
        addressVO.setENumber(enumber);
        addressVO.setLocation(location);
        return addressService.add(addressVO);

    }

    /**
     * 删除
     */
    @RequestMapping("/address_list/delete/{id}")
    @ResponseBody
    public Result deleteById(@PathVariable("id") Integer id)throws Exception{
        Result result = addressService.deleteById(id);
        Integer code = result.getCode();
        if (code==200){
            addressService.deleteById(id);
        }
        return result;
    }


    @RequestMapping("/address_list/update")
    @ResponseBody
    //更新
    public Result updateById(Integer id,String equipmentname,Integer enumber,String location)throws Exception{
        AddressVO addressVO=new AddressVO();
        addressVO.setId(id);
        addressVO.setEquipmentName(equipmentname);
        addressVO.setENumber(enumber);
        addressVO.setLocation(location);
        return addressService.updateById(addressVO);
    }
    /*更新回显
     * */
    @RequestMapping("/address_list/find/{id}")
    @ResponseBody
    public Result findClass(@PathVariable("id")Integer id)throws Exception{
        return addressService.findById(id);
    }

}
